package com.zingrow.common.utils;

import java.io.File;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;

import org.apache.commons.lang3.StringUtils;
import org.mongodb.morphia.Datastore;
import org.mongodb.morphia.Morphia;
import org.mongodb.morphia.aggregation.Accumulator;
import org.mongodb.morphia.aggregation.AggregationPipeline;
import org.mongodb.morphia.aggregation.Group;
import org.mongodb.morphia.query.Query;
import org.mongodb.morphia.query.Sort;
import org.mongodb.morphia.query.UpdateOperations;
import org.mongodb.morphia.query.UpdateResults;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.util.HtmlUtils;

import com.google.common.base.Splitter;
import com.google.common.base.Splitter.MapSplitter;
import com.mongodb.AggregationOptions;
import com.mongodb.MongoClient;
import com.mongodb.MongoClientOptions;
import com.mongodb.ServerAddress;
import com.mongodb.WriteResult;
import com.mongodb.client.MongoDatabase;
import com.zingrow.web.configure.distribute.model.InformationDistributeModel;

import org.apache.commons.collections.IteratorUtils;






/**
 * MongoDB连接
 * MongoDBConnection
 */
public class MongoDBConnection {
	
	Logger logger = LoggerFactory.getLogger(MongoDBConnection.class);
	
	public static MongoDBConnection insetance = null;
	
	private Datastore datastore;
	
	private MongoClient mongoClient;
	
	private MapSplitter mapSplitter = Splitter.onPattern(",|，").omitEmptyStrings().trimResults().withKeyValueSeparator(":");
	
	private Map<String, MongoDatabase> databaseInfos = new java.util.LinkedHashMap<String, MongoDatabase>();
	
	private Morphia morphia = new Morphia(); 
	
	private MongoDBConnection()
	{
		FileInputStream fis = null;
		try
		{
			morphia.mapPackage("com.zingrow.web.configure.distribute.model");
			fis = new FileInputStream(new File(MongoDBConnection.class.getResource("/").getPath(), "properties/mongo.properties"));
			Properties properties = new Properties();
			properties.load(fis);
			
			String databaseName = properties.getProperty("mongo_dbName");
			String hostList = properties.getProperty("mongo_host");
			Map<String, String> serverMap = mapSplitter.split(hostList);
			List<ServerAddress> serverList = new ArrayList<ServerAddress>();
			if(null!=serverMap && !serverMap.isEmpty())
			{
				for (Entry<String, String> entry : serverMap.entrySet())
				{
					serverList.add(new ServerAddress(entry.getKey(), Integer.parseInt(entry.getValue())));
				}
			}
			if(serverList.size()>0 && StringUtils.isNotBlank(databaseName))
			{
				MongoClientOptions.Builder builder = new MongoClientOptions.Builder();
				builder.minConnectionsPerHost(10);
				builder.connectionsPerHost(10);
				builder.connectTimeout(1000*60);
				builder.socketTimeout(1000*60);
				builder.maxWaitTime(1000*60);
				builder.threadsAllowedToBlockForConnectionMultiplier(5);
				builder.maxConnectionIdleTime(1000*60*60);
				builder.maxConnectionLifeTime(1000*60*60*2);
				mongoClient = new MongoClient(serverList, builder.build());
				datastore = morphia.createDatastore(mongoClient, StringUtils.strip(databaseName));
				datastore.ensureIndexes();
				logger.info("morphia连接池初始化完毕！");
			}
		} catch (Exception e)
		{
			e.printStackTrace();
			logger.error(e.getMessage());
		} finally
		{
			if(null!=fis)
			{
				try
				{
					fis.close();
				} catch (Exception e)
				{
					e.printStackTrace();
					logger.error(e.getMessage());
				}
			}
		}
	}
	
	static 
	{
		insetance = new MongoDBConnection();
	}
	
	public static MongoDBConnection getInstance()
	{
		return insetance;
	}
	
	public Datastore getDatastore()
	{
		return MongoDBConnection.getInstance().datastore;
	}
	
	private MongoClient getMongoClient()
	{
		return MongoDBConnection.getInstance().mongoClient;
	}
	
	//删除 废弃方法
	public boolean deleteByField(Class<?> clazz, String fieldName, String fieldValue,Date startTime,Date endTime){
		boolean b = false;
		//datastore.delete(datastore.createQuery(clazz).field(fieldName).equal(fieldValue));
		Query<?> query = datastore.createQuery(clazz).field("groupId").equal(fieldValue);
		if (null != startTime) {
			query.criteria("releaseTime").greaterThanOrEq(startTime);
		}
		if (null != endTime) {
			query.criteria("releaseTime").lessThan(endTime);
		}
		WriteResult result = datastore.delete(query);
		if(result.getN()>0) {
			b = true;
		}
		return b;
	}
	//删除
	public boolean deleteByField(Class<?> clazz,Map<String, Object> map,List<String> groupId) {
		boolean b = false;
		// 得到连接
		Datastore store = getDatastore();
		// 查询MongoDB数据库是否有这个实体数据
		Query<?> query = store.createQuery(clazz);
		// 判断map不为空
		if (null != map && !map.isEmpty()) {
			for (Entry<String, Object> entry : map.entrySet()) {		
				// 键
				String field = entry.getKey();
				// 值
				Object value = entry.getValue();
				if (null != value) {
					if (field.equalsIgnoreCase("startTime")) {
						Date startTime = (Date) value;
						query.criteria("releaseTime")
								.greaterThanOrEq(startTime);
					} else if (field.equalsIgnoreCase("endTime")) {
						Date endTime = (Date) value;
						query.criteria("releaseTime").lessThan(endTime);
					} else if (field.equalsIgnoreCase("idList")) {
						query.criteria("ifmId").equal(value);
					} else {
						if (StringUtils.isNotBlank((String) value)) {
							query.criteria(field).equals(value);
						}
					}
				}
			}
		}
		query.criteria("groupId").in(groupId);
		WriteResult result = datastore.delete(query);
		if (result.getN() > 0) {
			b = true;
		}
		
		return b;
	}
	// 多条件模糊查询
	@SuppressWarnings("unchecked")
	public List<InformationDistributeModel> findGroup(Class<?> modelClass,
			Map<String, Object> conditions, PageView pageView) {
		int pageSize = pageView.getPageSize();
		int pageIndex = pageView.getPageNow();
		List<String> slist = new ArrayList<String>();
		List<InformationDistributeModel> list = new ArrayList<InformationDistributeModel>();
		// 得到连接
		Datastore store = getDatastore();
		// 查询MongoDB数据库是否有这个实体数据
		Query<?> query = store.createQuery(modelClass);
		// 数组
		Object[] criteria = new Object[conditions.size()];
		// 判断map不为空
		if (null != conditions && !conditions.isEmpty()) {
			int i = 0;
			if (conditions.containsKey("title")
					&& StringUtils.isNotBlank((String) conditions.get("title"))) {// 非微博查询
				for (Entry<String, Object> entry : conditions.entrySet()) {
					// 键
					String field = entry.getKey();
					// 值
					Object value = entry.getValue();
					if (null != value) {
						if (field.equalsIgnoreCase("startTime")) {
							Date startTime = (Date) value;
							criteria[i] = query.criteria("releaseTime")
									.greaterThanOrEq(startTime);
						} else if (field.equalsIgnoreCase("endTime")) {
							Date endTime = (Date) value;
							criteria[i] = query.criteria("releaseTime")
									.lessThan(endTime);
						} else if (field.equalsIgnoreCase("idList")) {
							List<String> idList = (List<String>) value;
							criteria[i] = query.criteria("ifmId").in(idList);
						} else {
							if (StringUtils.isNotBlank((String) value)) {
								criteria[i] = query.criteria(field).contains(
										(String) value);
							}
						}
					}
				}
			} else {// 微博或者新闻查询
				for (Entry<String, Object> entry : conditions.entrySet()) {
					// 键
					String field = entry.getKey();
					// 值
					Object value = entry.getValue();
					if (null != value) {
						if (field.equalsIgnoreCase("startTime")) {
							Date startTime = (Date) value;
							criteria[i] = query.criteria("releaseTime")
									.greaterThanOrEq(startTime);
						} else if (field.equalsIgnoreCase("endTime")) {
							Date endTime = (Date) value;
							criteria[i] = query.criteria("releaseTime")
									.lessThan(endTime);
						} else if (field.equalsIgnoreCase("idList")) {
							List<String> idList = (List<String>) value;
							criteria[i] = query.criteria("ifmId").in(idList);
						} else {
							if (StringUtils.isNotBlank((String) value)) {
								if (field.equalsIgnoreCase("content")) {
									query.or(query.criteria("content").containsIgnoreCase((String)value),
											query.criteria("weiboContent").containsIgnoreCase((String)value),
											query.criteria("weiboSourceContent").containsIgnoreCase((String)value));
								} else if (field
										.equalsIgnoreCase("websiteName")) {
									criteria[i] = query.or(
											query.criteria("websiteName")
													.containsIgnoreCase(
															(String) value),
											query.criteria("weiboBlogger")
													.containsIgnoreCase(
															(String) value));
								} else {
									
									criteria[i] = query.criteria(field)
											.contains((String) value);
								}
							}
						}
					}
				}
			}
		}
		//得到总数
		AggregationPipeline pelinetest = store
				.createAggregation(modelClass).match(query)
				.group(Group.id(Group.grouping("groupId")),Group.grouping("initcount",new Accumulator("$first", 1)))
				.group(Group.id(Group.grouping("groupId")),Group.grouping("count", Group.sum("initcount")));
		Iterator<?> itercs = pelinetest.aggregate(modelClass);
		long totalcount = 0;
		while (itercs.hasNext()) {
			InformationDistributeModel buteModel = (InformationDistributeModel) itercs
					.next();
			totalcount = buteModel.getCount();
		}
		//排序
		query.order(Sort.descending("sort_timestamp"));
		//先根据组织ID聚合查询最新数据pageSize条，排序
		AggregationPipeline line = store.createAggregation(modelClass).match(query).sort(Sort.descending("sort_timestamp"),Sort.descending("releaseTime"))
				.group("groupId"
						,Group.grouping("sort_timestamp",Group.first("sort_timestamp"))
						,Group.grouping("releaseTime",Group.first("releaseTime")),
						Group.grouping("groupId",Group.first("groupId")))
						.sort(Sort.descending("sort_timestamp"),Sort.descending("releaseTime"))
						.skip(pageIndex * pageSize - pageSize).limit(pageSize);
		//得到查询集合中的groupId
		Iterator<?> iter = line.aggregate(modelClass,AggregationOptions.builder().allowDiskUse(true).build());
		while(iter.hasNext()){
			InformationDistributeModel ls = (InformationDistributeModel)iter.next();
			slist.add(ls.getGroupId());
		}
		//根据query中的条件和加入的多个groupId第二次聚合查询出其他字段数据
		if (null != slist) {
			query.order(Sort.descending("sort_timestamp")).field("groupId").in(slist);
			AggregationPipeline pipeline = store.createAggregation(modelClass).match(query).sort(Sort.descending("sort_timestamp"),Sort.descending("releaseTime"))
					.group(Group.id(Group.grouping("groupId")),Group.grouping("groupId",Group.first("GroupId")),
							Group.grouping("sort_timestamp",Group.first("sort_timestamp")),
							Group.grouping("ifmId", Group.first("ifmId")),
							Group.grouping("ifmName", Group.first("ifmName")),
							Group.grouping("articleId", Group.first("articleId")),
							Group.grouping("articleType",Group.first("articleType")),
							Group.grouping("url", Group.first("url")),
							Group.grouping("releaseTime",Group.first("releaseTime")),
							Group.grouping("title", Group.first("title")),
							Group.grouping("readNumb", Group.first("readNumb")),
							Group.grouping("replyNumb", Group.first("replyNumb")),
							Group.grouping("websiteSign",Group.first("websiteSign")),
							Group.grouping("pointNum", Group.first("pointNum")),
							Group.grouping("websiteName",Group.first("websiteName")),
							Group.grouping("weiboType", Group.first("weiboType")),
							Group.grouping("weiboReprintNumb",Group.first("weiboReprintNumb")),
							Group.grouping("weiboCommentNumb",Group.first("weiboCommentNumb")),
							Group.grouping("weiboIsForward",Group.first("weiboIsForward")),
							Group.grouping("groupId",Group.first("groupId")),
							Group.grouping("weiboSourceContent",Group.first("weiboSourceContent")),
							Group.grouping("weiboContent",Group.first("weiboContent")),
							Group.grouping("weiboBlogger",Group.first("weiboBlogger")),
							Group.grouping("isCollection",Group.first("isCollection")),
							Group.grouping("reprintNumb",new Accumulator("$sum", 1)),
							Group.grouping("content",Group.first("content")),
							Group.grouping("weiboSourceBlogger",Group.first("weiboSourceBlogger")),
							Group.grouping("groupSign", Group.first("groupSign")),
							Group.grouping("timestamp", Group.first("timestamp"))).sort(Sort.descending("sort_timestamp"));
			
			Iterator<?> iterator = pipeline.aggregate(modelClass,AggregationOptions.builder().allowDiskUse(true).build());
			
			// 查询相似的
			while (iterator.hasNext()) {
				InformationDistributeModel buteModel = (InformationDistributeModel) iterator.next();
				Query<?> querymm = store.createQuery(modelClass);
				// 判断map不为空
				if (null != conditions && !conditions.isEmpty()) {
					int i = 0;
					if (conditions.containsKey("title")
							&& StringUtils.isNotBlank((String) conditions.get("title"))) {// 非微博查询
						for (Entry<String, Object> entry : conditions.entrySet()) {
							// 键
							String field = entry.getKey();
							// 值
							Object value = entry.getValue();
							if (null != value) {
								if (field.equalsIgnoreCase("startTime")) {
									Date startTime = (Date) value;
									criteria[i] = querymm.criteria("releaseTime")
											.greaterThanOrEq(startTime);
								} else if (field.equalsIgnoreCase("endTime")) {
									Date endTime = (Date) value;
									criteria[i] = querymm.criteria("releaseTime")
											.lessThan(endTime);
								} else if (field.equalsIgnoreCase("idList")) {
									List<String> idList = (List<String>) value;
									criteria[i] = querymm.criteria("ifmId").in(idList);
								} else {
									if (StringUtils.isNotBlank((String) value)) {
										criteria[i] = querymm.criteria(field).contains(
												(String) value);
									}
								}
							}
						}
					} else {// 微博或者新闻查询
						for (Entry<String, Object> entry : conditions.entrySet()) {
							// 键
							String field = entry.getKey();
							// 值
							Object value = entry.getValue();
							if (null != value) {
								if (field.equalsIgnoreCase("startTime")) {
									Date startTime = (Date) value;
									criteria[i] = querymm.criteria("releaseTime")
											.greaterThanOrEq(startTime);
								} else if (field.equalsIgnoreCase("endTime")) {
									Date endTime = (Date) value;
									criteria[i] = querymm.criteria("releaseTime")
											.lessThan(endTime);
								} else if (field.equalsIgnoreCase("idList")) {
									List<String> idList = (List<String>) value;
									criteria[i] = querymm.criteria("ifmId").in(idList);
								} else {
									if (StringUtils.isNotBlank((String) value)) {
										if (field.equalsIgnoreCase("content")) {
											querymm.or(querymm.criteria("content").containsIgnoreCase((String) value),
													querymm.criteria("weiboContent").containsIgnoreCase((String) value),
													querymm.criteria("weiboSourceContent").containsIgnoreCase((String) value));
										} else if (field
												.equalsIgnoreCase("websiteName")) {
											criteria[i] = querymm.or(
													querymm.criteria("websiteName")
															.containsIgnoreCase(
																	(String) value),
													querymm.criteria("weiboBlogger")
															.containsIgnoreCase(
																	(String) value));
										} else {

											criteria[i] = querymm.criteria(field)
													.contains((String) value);
										}
									}
								}
							}
						}
					}
				}
				List<InformationDistributeModel> listbute = new ArrayList<InformationDistributeModel>();
				querymm.criteria("groupId").equal(buteModel.getGroupId());
				Iterator<?> tor = querymm.order(Sort.descending("sort_timestamp")).iterator();
				while (tor.hasNext()) {
					InformationDistributeModel dist = (InformationDistributeModel) tor.next();
					if (dist.getContent() != null && !dist.getContent().equals("")) {
						dist.setContent(dist.getContent().replaceAll("　| ", "")
								.replaceAll("</?[^>]+>", ""));
						// 转译
						dist.setContent(HtmlUtils.htmlEscape(dist
								.getContent()));
						dist.setContent(dist.getContent().replaceAll("\\&[a-zA-Z]{1,10};", ""));
					} else if (dist.getWeiboSourceContent() != null
							&& !dist.getWeiboSourceContent().equals("")) {
						dist.setContent(dist.getWeiboSourceContent()
								.replaceAll("　| ", "").replaceAll("</?[^>]+>", ""));
						//转义
						dist.setWeiboSourceContent(HtmlUtils.htmlEscape(dist.getWeiboSourceContent()));
						dist.setWebsiteName(dist.getWeiboBlogger().replaceAll("\\&[a-zA-Z]{1,10};", ""));
					} else if (dist.getWeiboContent() != null
							&& !dist.getWeiboContent().equals("")) {
						dist.setContent(dist.getWeiboContent()
								.replaceAll("　| ", "").replaceAll("</?[^>]+>", ""));
						//转义
						dist.setWeiboContent(HtmlUtils.htmlEscape(dist.getWeiboContent()));
						dist.setWebsiteName(dist.getWeiboBlogger().replaceAll("\\&[a-zA-Z]{1,10};", ""));
					}
					if(!buteModel.getIfmId().equals(dist.getIfmId())){
						if (buteModel.getArticleId().equals(dist.getArticleId())) {
							listbute.add(dist);
						}else{
							listbute.add(dist);
						}
					}else{
						if (!buteModel.getArticleId().equals(dist.getArticleId())) {
							listbute.add(dist);
						}
					}
				}
				buteModel.setReprintList(listbute);

				// 根据articleId查询改数据的ID
				Query<?> querybute = store.createQuery(modelClass);
				querybute.criteria("articleId").equal(buteModel.getArticleId());
				Iterator<?> iterator2 = querybute.iterator();
				while(iterator2.hasNext()){
					InformationDistributeModel dist = (InformationDistributeModel)iterator2.next();
					if(dist.getIfmId().equals(buteModel.getIfmId())&&dist.getArticleId().equals(buteModel.getArticleId())){
						buteModel.setId(dist.getId());
					}
				}
				buteModel.setCount(totalcount);
				list.add(buteModel);
			}
		}
		return list;
	}

	// 多条件模糊查询
	/*@SuppressWarnings("unchecked")
	public List<InformationDistributeModel> findGroup(Class<?> modelClass,
			Map<String, Object> conditions, PageView pageView) {
		int pageSize = pageView.getPageSize();
		int pageIndex = pageView.getPageNow();
		List<String> slist = new ArrayList<String>();
		List<InformationDistributeModel> list = new ArrayList<InformationDistributeModel>();
		// 得到连接
		Datastore store = getDatastore();
		// 查询MongoDB数据库是否有这个实体数据
		Query<?> query = store.createQuery(modelClass);
		// 数组
		Object[] criteria = new Object[conditions.size()];
		// 判断map不为空
		if (null != conditions && !conditions.isEmpty()) {
			int i = 0;
			if (conditions.containsKey("title")
					&& StringUtils.isNotBlank((String) conditions.get("title"))) {// 非微博查询
				for (Entry<String, Object> entry : conditions.entrySet()) {
					// 键
					String field = entry.getKey();
					// 值
					Object value = entry.getValue();
					if (null != value) {
						if (field.equalsIgnoreCase("startTime")) {
							Date startTime = (Date) value;
							criteria[i] = query.criteria("releaseTime")
									.greaterThanOrEq(startTime);
						} else if (field.equalsIgnoreCase("endTime")) {
							Date endTime = (Date) value;
							criteria[i] = query.criteria("releaseTime")
									.lessThanOrEq(endTime);
						} else if (field.equalsIgnoreCase("idList")) {
							List<String> idList = (List<String>) value;
							criteria[i] = query.criteria("ifmId").in(idList);
						} else {
							if (StringUtils.isNotBlank((String) value)) {
								criteria[i] = query.criteria(field).contains(
										(String) value);
							}
						}
					}
				}
			} else {// 微博或者新闻查询
				for (Entry<String, Object> entry : conditions.entrySet()) {
					// 键
					String field = entry.getKey();
					// 值
					Object value = entry.getValue();
					if (null != value) {
						if (field.equalsIgnoreCase("startTime")) {
							Date startTime = (Date) value;
							criteria[i] = query.criteria("releaseTime")
									.greaterThanOrEq(startTime);
						} else if (field.equalsIgnoreCase("endTime")) {
							Date endTime = (Date) value;
							criteria[i] = query.criteria("releaseTime")
									.lessThanOrEq(endTime);
						} else if (field.equalsIgnoreCase("idList")) {
							List<String> idList = (List<String>) value;
							criteria[i] = query.criteria("ifmId").in(idList);
						} else {
							if (StringUtils.isNotBlank((String) value)) {
								if (field.equalsIgnoreCase("content")) {
									query.or(
											query.criteria("content")
													.containsIgnoreCase(
															(String) value),
											query.criteria("weiboContent")
													.containsIgnoreCase(
															(String) value),
											query.criteria("weiboSourceContent")
													.containsIgnoreCase(
															(String) value));
								} else if (field
										.equalsIgnoreCase("websiteName")) {
									criteria[i] = query.or(
											query.criteria("websiteName")
													.containsIgnoreCase(
															(String) value),
											query.criteria("weiboBlogger")
													.containsIgnoreCase(
															(String) value));
								} else {

									criteria[i] = query.criteria(field)
											.contains((String) value);
								}
							}
						}
					}
				}
			}
		}
		// 得到总数
		AggregationPipeline pelinetest = store
				.createAggregation(modelClass)
				.match(query)
				.group(Group.id(Group.grouping("groupId")),
						Group.grouping("initcount",
								new Accumulator("$first", 1)))
				.group(Group.id(Group.grouping("groupId")),
						Group.grouping("count", Group.sum("initcount")));
		Iterator<?> itercs = pelinetest.aggregate(modelClass,
				AggregationOptions.builder().allowDiskUse(true).build());
		long totalcount = 0;
		while (itercs.hasNext()) {
			InformationDistributeModel buteModel = (InformationDistributeModel) itercs
					.next();
			totalcount = buteModel.getCount();
		}
		// 排序
		query.order(Sort.descending("sort_timestamp"));
		// 先根据组织ID聚合查询最新数据pageSize条，排序
		AggregationPipeline line = store
				.createAggregation(modelClass)
				.match(query)
				.sort(Sort.descending("sort_timestamp"),
						Sort.descending("releaseTime"))
				.group("groupId",
						Group.grouping("sort_timestamp",
								Group.first("sort_timestamp")),
						Group.grouping("releaseTime",
								Group.first("releaseTime")),
						Group.grouping("groupId", Group.last("groupId")))
				.sort(Sort.descending("sort_timestamp"),
						Sort.descending("releaseTime"))
				.skip(pageIndex * pageSize - pageSize).limit(pageSize);
		// 得到查询集合中的groupId
		Iterator<?> iter = line.aggregate(modelClass,
				AggregationOptions.builder().allowDiskUse(true).build());
		while (iter.hasNext()) {
			InformationDistributeModel ls = (InformationDistributeModel) iter
					.next();
			slist.add(ls.getGroupId());
		}
		// 根据query中的条件和加入的多个groupId第二次聚合查询出其他字段数据
		query.order(Sort.descending("sort_timestamp")).field("groupId")
				.in(slist);
		AggregationPipeline pipeline = store
				.createAggregation(modelClass)
				.match(query)
				.sort(Sort.descending("sort_timestamp"),
						Sort.descending("releaseTime"))
				.group(Group.id(Group.grouping("groupId")),
						Group.grouping("groupId", Group.first("GroupId")),
						Group.grouping("sort_timestamp",
								Group.first("sort_timestamp")),
						Group.grouping("ifmId", Group.first("ifmId")),
						Group.grouping("ifmName", Group.first("ifmName")),
						Group.grouping("articleId", Group.first("articleId")),
						Group.grouping("articleType",
								Group.first("articleType")),
						Group.grouping("url", Group.first("url")),
						Group.grouping("releaseTime",
								Group.first("releaseTime")),
						Group.grouping("title", Group.first("title")),
						Group.grouping("readNumb", Group.first("readNumb")),
						Group.grouping("replyNumb", Group.first("replyNumb")),
						Group.grouping("websiteSign",
								Group.first("websiteSign")),
						Group.grouping("pointNum", Group.first("pointNum")),
						Group.grouping("websiteName",
								Group.first("websiteName")),
						Group.grouping("weiboType", Group.first("weiboType")),
						Group.grouping("weiboReprintNumb",
								Group.first("weiboReprintNumb")),
						Group.grouping("weiboCommentNumb",
								Group.first("weiboCommentNumb")),
						Group.grouping("weiboIsForward",
								Group.first("weiboIsForward")),
						Group.grouping("groupId", Group.first("groupId")),
						Group.grouping("weiboSourceContent",
								Group.first("weiboSourceContent")),
						Group.grouping("weiboContent",
								Group.first("weiboContent")),
						Group.grouping("weiboBlogger",
								Group.first("weiboBlogger")),
						Group.grouping("isCollection",
								Group.first("isCollection")),
						Group.grouping("reprintNumb",
								new Accumulator("$sum", 1)),
						Group.grouping("content", Group.first("content")),
						Group.grouping("weiboSourceBlogger",
								Group.first("weiboSourceBlogger")),
						Group.grouping("groupSign", Group.first("groupSign")),
						Group.grouping("timestamp", Group.first("timestamp")))
				.sort(Sort.descending("sort_timestamp"));

		Iterator<?> iterator = pipeline.aggregate(modelClass,
				AggregationOptions.builder().allowDiskUse(true).build());

		// 查询相似的
		while (iterator.hasNext()) {
			InformationDistributeModel buteModel = (InformationDistributeModel) iterator
					.next();
			Query<?> querymm = store.createQuery(modelClass);
			// 判断map不为空
			if (null != conditions && !conditions.isEmpty()) {
				int i = 0;
				if (conditions.containsKey("title")
						&& StringUtils.isNotBlank((String) conditions
								.get("title"))) {// 非微博查询
					for (Entry<String, Object> entry : conditions.entrySet()) {
						// 键
						String field = entry.getKey();
						// 值
						Object value = entry.getValue();
						if (null != value) {
							if (field.equalsIgnoreCase("startTime")) {
								Date startTime = (Date) value;
								criteria[i] = querymm.criteria("releaseTime")
										.greaterThanOrEq(startTime);
							} else if (field.equalsIgnoreCase("endTime")) {
								Date endTime = (Date) value;
								criteria[i] = querymm.criteria("releaseTime")
										.lessThanOrEq(endTime);
							} else if (field.equalsIgnoreCase("idList")) {
								List<String> idList = (List<String>) value;
								criteria[i] = querymm.criteria("ifmId").in(
										idList);
							} else {
								if (StringUtils.isNotBlank((String) value)) {
									criteria[i] = querymm.criteria(field)
											.contains((String) value);
								}
							}
						}
					}
				} else {// 微博或者新闻查询
					for (Entry<String, Object> entry : conditions.entrySet()) {
						// 键
						String field = entry.getKey();
						// 值
						Object value = entry.getValue();
						if (null != value) {
							if (field.equalsIgnoreCase("startTime")) {
								Date startTime = (Date) value;
								criteria[i] = querymm.criteria("releaseTime")
										.greaterThanOrEq(startTime);
							} else if (field.equalsIgnoreCase("endTime")) {
								Date endTime = (Date) value;
								criteria[i] = querymm.criteria("releaseTime")
										.lessThanOrEq(endTime);
							} else if (field.equalsIgnoreCase("idList")) {
								List<String> idList = (List<String>) value;
								criteria[i] = querymm.criteria("ifmId").in(
										idList);
							} else {
								if (StringUtils.isNotBlank((String) value)) {
									if (field.equalsIgnoreCase("content")) {
										querymm.or(
												querymm.criteria("content")
														.containsIgnoreCase(
																(String) value),
												querymm.criteria("weiboContent")
														.containsIgnoreCase(
																(String) value),
												querymm.criteria(
														"weiboSourceContent")
														.containsIgnoreCase(
																(String) value));
									} else if (field
											.equalsIgnoreCase("websiteName")) {
										criteria[i] = querymm
												.or(querymm.criteria(
														"websiteName")
														.containsIgnoreCase(
																(String) value),
														querymm.criteria(
																"weiboBlogger")
																.containsIgnoreCase(
																		(String) value));
									} else {

										criteria[i] = querymm.criteria(field)
												.contains((String) value);
									}
								}
							}
						}
					}
				}
			}
			List<InformationDistributeModel> listbute = new ArrayList<InformationDistributeModel>();
			query.criteria("groupId").equal(buteModel.getGroupId());
			Iterator<?> tor = query.order(Sort.descending("sort_timestamp"))
					.iterator();
			while (tor.hasNext()) {
				InformationDistributeModel dist = (InformationDistributeModel) tor
						.next();
				if (dist.getContent() != null && !dist.getContent().equals("")) {
					dist.setContent(dist.getContent().replaceAll("　| ", "")
							.replaceAll("</?[^>]+>", "")
							.replaceAll("&nbsp;", ""));
					// 转译
					dist.setContent(HtmlUtils.htmlEscape(dist.getContent()));
				} else if (dist.getWeiboSourceContent() != null
						&& !dist.getWeiboSourceContent().equals("")) {
					dist.setContent(dist.getWeiboSourceContent()
							.replaceAll("　| ", "").replaceAll("</?[^>]+>", "")
							.replaceAll("&nbsp;", ""));
					// 转义
					dist.setWeiboSourceContent(HtmlUtils.htmlEscape(dist
							.getWeiboSourceContent()));
					dist.setWebsiteName(dist.getWeiboBlogger());
				} else if (dist.getWeiboContent() != null
						&& !dist.getWeiboContent().equals("")) {
					dist.setContent(dist.getWeiboContent()
							.replaceAll("　| ", "").replaceAll("</?[^>]+>", "")
							.replaceAll("&nbsp;", ""));
					// 转义
					dist.setWeiboContent(HtmlUtils.htmlEscape(dist
							.getWeiboContent()));
					dist.setWebsiteName(dist.getWeiboBlogger());
				}
				if (!buteModel.getIfmId().equals(dist.getIfmId())) {
					if (buteModel.getArticleId().equals(dist.getArticleId())) {
						listbute.add(dist);
					} else {
						listbute.add(dist);
					}
				} else {
					if (!buteModel.getArticleId().equals(dist.getArticleId())) {
						listbute.add(dist);
					}
				}
			}
			buteModel.setReprintList(listbute);

			// 根据articleId查询改数据的ID
			Query<?> querybute = store.createQuery(modelClass);
			querybute.criteria("articleId").equal(buteModel.getArticleId());
			Iterator<?> iterator2 = querybute.iterator();
			while (iterator2.hasNext()) {
				InformationDistributeModel dist = (InformationDistributeModel) iterator2
						.next();
				if (dist.getIfmId().equals(buteModel.getIfmId())
						&& dist.getArticleId().equals(buteModel.getArticleId())) {
					buteModel.setId(dist.getId());
				}
			}
			buteModel.setCount(totalcount);
			list.add(buteModel);
		}
		return list;
	}*/
	// 修改
	public boolean Update(InformationDistributeModel bute) {
		boolean result = false;
		Query<InformationDistributeModel> query = getDatastore().createQuery(
				InformationDistributeModel.class);
		query.criteria("articleId").equal(bute.getArticleId());
		UpdateOperations<InformationDistributeModel> ops = getDatastore()
				.createUpdateOperations(InformationDistributeModel.class);
		ops.set("title", bute.getTitle());
		ops.set("websiteName", bute.getWebsiteName());
		ops.set("author", bute.getAuthor());
		ops.set("content", bute.getContent());
		ops.set("articleType", bute.getArticleType());
		ops.set("url", bute.getUrl());
		UpdateResults sp = getDatastore().update(query, ops);
		if (sp.getUpdatedExisting()) {
			result = true;
		}
		return result;
	}

	// 删除
	public boolean delete(Class<?> InformationDistributeModel,
			String articleId, String ifmId) {
		boolean result = false;
		Query<?> query = getDatastore().createQuery(InformationDistributeModel);
		query.and(query.criteria("articleId").equal(articleId),query.criteria("ifmId").equal(ifmId));
		WriteResult writeResult = getDatastore().delete(query);
		if (writeResult != null) {
			result = true;
		}
		return result;
	}

	// 根据多个id查询数据--导出列表
	public List<InformationDistributeModel> findAllid(
			Class<?> InformationDistributeModel, List<String> ids) {
		List<InformationDistributeModel> list = new ArrayList<InformationDistributeModel>();
		Query<?> query = getDatastore().createQuery(InformationDistributeModel);
		Iterator<?> iter = null;
		if (ids != null && !ids.equals("")) {
			query.criteria("_id").in(ids);
			query.order(Sort.descending("sort_timestamp"));
			iter = query.iterator();
		}
		if (iter != null) {
			while (iter.hasNext()) {
				InformationDistributeModel model = (InformationDistributeModel) iter.next();
				list.add(model);
			}
		}		return list;
	}

	// 根据条件查询数据--导出所有数据
	public List<InformationDistributeModel> findGroupAll(
			Class<?> InformationDistributeModel, Map<String, Object> conditions) {
		List<InformationDistributeModel> list = new ArrayList<InformationDistributeModel>();
		// 得到连接
		Datastore store = getDatastore();
		// 查询MongoDB数据库是否有这个实体数据
		Query<?> query = store.createQuery(InformationDistributeModel);
		// 数组
		Object[] criteria = new Object[conditions.size()];
		// 判断map不为空
		if (null != conditions && !conditions.isEmpty()) {
			int i = 0;
			if (conditions.containsKey("title")
					&& StringUtils.isNotBlank((String) conditions.get("title"))) {// 非微博查询
				for (Entry<String, Object> entry : conditions.entrySet()) {
					// 键
					String field = entry.getKey();
					// 值
					Object value = entry.getValue();
					if (null != value) {
						if (field.equalsIgnoreCase("startTime")) {
							Date startTime = (Date) value;
							criteria[i] = query.criteria("releaseTime")
									.greaterThanOrEq(startTime);
						} else if (field.equalsIgnoreCase("endTime")) {
							Date endTime = (Date) value;
							criteria[i] = query.criteria("releaseTime")
									.lessThanOrEq(endTime);
						} else if (field.equalsIgnoreCase("idList")) {
							List<String> idList = (List<String>) value;
							criteria[i] = query.criteria("ifmId").in(idList);
						} else {
							if (StringUtils.isNotBlank((String) value)) {
								criteria[i] = query.criteria(field).contains(
										(String) value);
							}
						}
					}
				}
			} else {// 微博或者新闻查询
				for (Entry<String, Object> entry : conditions.entrySet()) {
					// 键
					String field = entry.getKey();
					// 值
					Object value = entry.getValue();
					if (null != value) {
						if (field.equalsIgnoreCase("startTime")) {
							Date startTime = (Date) value;
							criteria[i] = query.criteria("releaseTime")
									.greaterThanOrEq(startTime);
						} else if (field.equalsIgnoreCase("endTime")) {
							Date endTime = (Date) value;
							criteria[i] = query.criteria("releaseTime")
									.lessThanOrEq(endTime);
						} else if (field.equalsIgnoreCase("idList")) {
							List<String> idList = (List<String>) value;
							criteria[i] = query.criteria("ifmId").in(idList);
						} else {
							if (StringUtils.isNotBlank((String) value)) {
								if (field.equalsIgnoreCase("content")) {
									criteria[i] = query.search((String) value);
								} else if (field
										.equalsIgnoreCase("websiteName")) {
									criteria[i] = query.or(
											query.criteria("websiteName")
													.containsIgnoreCase(
															(String) value),
											query.criteria("weiboBlogger")
													.containsIgnoreCase(
															(String) value));

								} else {
									criteria[i] = query.criteria(field)
											.contains((String) value);
								}
							}
						}
					}
				}
			}
		}
		AggregationPipeline peline = store
				.createAggregation(InformationDistributeModel)
				.match(query)
				.group(Group.id(Group.grouping("groupId")),
						Group.grouping("groupId", Group.first("groupId")),
						Group.grouping("ifmId", Group.first("ifmId")),
						Group.grouping("ifmName", Group.first("ifmName")),
						Group.grouping("articleId", Group.first("articleId")),
						Group.grouping("articleType",
								Group.first("articleType")),
						Group.grouping("url", Group.first("url")),
						Group.grouping("releaseTime",
								Group.first("releaseTime")),
						Group.grouping("title", Group.first("title")),
						Group.grouping("readNumb", Group.first("readNumb")),
						Group.grouping("replyNumb", Group.first("replyNumb")),
						Group.grouping("websiteSign",
								Group.first("websiteSign")),
						Group.grouping("pointNum", Group.first("pointNum")),
						Group.grouping("websiteName",
								Group.first("websiteName")),
						Group.grouping("weiboType", Group.first("weiboType")),
						Group.grouping("weiboReprintNumb",
								Group.first("weiboReprintNumb")),
						Group.grouping("weiboCommentNumb",
								Group.first("weiboCommentNumb")),
						Group.grouping("weiboIsForward",
								Group.first("weiboIsForward")),
						Group.grouping("weiboSourceBlogger",
								Group.first("weiboSourceBlogger")),
						Group.grouping("weiboSourceContent",
								Group.first("weiboSourceContent")),
						Group.grouping("weiboContent",
								Group.first("weiboContent")),
						Group.grouping("weiboBlogger",
								Group.first("weiboBlogger")),
						Group.grouping("isCollection",
								Group.first("isCollection")),
						Group.grouping("groupSign", Group.first("groupSign")),
						Group.grouping("timestamp", Group.first("timestamp")),
						Group.grouping("content", Group.first("content")),
						Group.grouping("sort_timestamp",
								Group.first("sort_timestamp")))
				.sort(Sort.descending("sort_timestamp"));

		Iterator<?> iter = peline.aggregate(InformationDistributeModel,
				AggregationOptions.builder().allowDiskUse(true).build());
		while (iter.hasNext()) {
			InformationDistributeModel bute = (InformationDistributeModel) iter
					.next();
			list.add(bute);
		}
		return list;
	}

	// 根据多个id查询数据--导出文章
	public List<InformationDistributeModel> findwordId(
			Class<?> InformationDistributeModel, List<String> ids) {
		List<InformationDistributeModel> list = new ArrayList<InformationDistributeModel>();
		Query<?> query = getDatastore().createQuery(InformationDistributeModel);
		Iterator<?> iter = null;
		if (ids != null && !ids.equals("")) {
			query.criteria("_id").in(ids);
			query.order(Sort.descending("sort_timestamp"));
			iter = query.iterator();
		}
		if (null != iter) {
			while (iter.hasNext()) {
				InformationDistributeModel model = (InformationDistributeModel) iter
						.next();
				list.add(model);
			}
		}
		return list;
	}

	/*// 查询相似
	public List<InformationDistributeModel> findlike(
			Class<?> InformationDistributeModel, String groupId) {
		List<InformationDistributeModel> list = new ArrayList<InformationDistributeModel>();
		// 得到连接
		Datastore store = getDatastore();
		Query<?> groupQuery = store.createQuery(InformationDistributeModel);
		groupQuery.criteria("groupId").equal(groupId);
		AggregationPipeline pipeline = store.createAggregation(InformationDistributeModel)
				.match(groupQuery).sort(Sort.ascending("sort_timestamp"));
		Iterator<?> iterator = pipeline.aggregate(InformationDistributeModel,AggregationOptions.builder().allowDiskUse(true).build());
		while (iterator.hasNext()) {
			InformationDistributeModel model = (InformationDistributeModel) iterator.next();
			list.add(model);
			model.setReprintNumb(String.valueOf(list.size()));
		}
		return list;
	}*/
	
	// 查询相似
	public List<InformationDistributeModel> findlike(
			Class<?> InformationDistributeModel,String groupId,Map<String, Object> conditions) {
		List<InformationDistributeModel> list = new ArrayList<InformationDistributeModel>();
		// 数组
		Object[] criteria = new Object[conditions.size()];
		// 得到连接
		Datastore store = getDatastore();
		Query<?> query = store.createQuery(InformationDistributeModel);
		// 判断map不为空
		if (null != conditions && !conditions.isEmpty()) {
			int i = 0;
			if (conditions.containsKey("title")
					&& StringUtils.isNotBlank((String) conditions.get("title"))) {// 非微博查询
				for (Entry<String, Object> entry : conditions.entrySet()) {
					// 键
					String field = entry.getKey();
					// 值
					Object value = entry.getValue();
					if (null != value) {
						if (field.equalsIgnoreCase("startTime")) {
							Date startTime = (Date) value;
							criteria[i] = query.criteria("releaseTime")
									.greaterThanOrEq(startTime);
						} else if (field.equalsIgnoreCase("endTime")) {
							Date endTime = (Date) value;
							criteria[i] = query.criteria("releaseTime")
									.lessThanOrEq(endTime);
						} else if (field.equalsIgnoreCase("ids")) {
							List<String> idList = (List<String>) value;
							criteria[i] = query.criteria("ifmId").in(idList);
						} else {
							if (StringUtils.isNotBlank((String) value)) {
								criteria[i] = query.criteria(field).contains(
										(String) value);
							}
						}
					}
				}
			} else {// 微博或者新闻查询
				for (Entry<String, Object> entry : conditions.entrySet()) {
					// 键
					String field = entry.getKey();
					// 值
					Object value = entry.getValue();
					if (null != value) {
						if (field.equalsIgnoreCase("startTime")) {
							Date startTime = (Date) value;
							criteria[i] = query.criteria("releaseTime")
									.greaterThanOrEq(startTime);
						} else if (field.equalsIgnoreCase("endTime")) {
							Date endTime = (Date) value;
							criteria[i] = query.criteria("releaseTime")
									.lessThanOrEq(endTime);
						} else if (field.equalsIgnoreCase("ids")) {
							List<String> idList = (List<String>) value;
							criteria[i] = query.criteria("ifmId").in(idList);
						} else {
							if (StringUtils.isNotBlank((String) value)) {
								if (field.equalsIgnoreCase("content")) {
									query.or(query.criteria("content").containsIgnoreCase((String) value),
											query.criteria("weiboContent").containsIgnoreCase((String) value),
											query.criteria("weiboSourceContent").containsIgnoreCase((String)value));
								} else if (field
										.equalsIgnoreCase("websiteName")) {
									criteria[i] = query.or(
											query.criteria("websiteName")
													.containsIgnoreCase(
															(String) value),
											query.criteria("weiboBlogger")
													.containsIgnoreCase(
															(String) value));
								} else {

									criteria[i] = query.criteria(field)
											.contains((String) value);
								}
							}
						}
					}
				}
			}
		}
		query.criteria("groupId").equal(groupId);
		Iterator<?> iterator = query.order(Sort.ascending("sort_timestamp")).iterator();
		while(iterator.hasNext()){
			InformationDistributeModel model = (InformationDistributeModel)iterator.next();
			list.add(model);
			//model.setReprintNumb(String.valueOf(list.size()));
		}
		return list;
	}
	/*public List<InformationDistributeModel> findlikeone(Class<?> InformationDistributeModel, String groupId){
		List<InformationDistributeModel> list = new ArrayList<InformationDistributeModel>();
		// 得到连接
		Datastore store = getDatastore();
		Query<?> query = store.createQuery(InformationDistributeModel);
		Iterator<?> iterator = query.order(Sort.descending("sort_timestamp")).iterator();
		while(iterator.hasNext()){
			InformationDistributeModel model = (InformationDistributeModel) iterator.next();
			list.add(model);
			model.setReprintNumb(String.valueOf(list.size()));
		}
		return list;
	}*/
	
			/*//查询多少条数据
			public Integer selectcount(Class<?> InformationDistributeModel){
				Query<?> query = getDatastore().createQuery(InformationDistributeModel);
				long countt = query.count();
				int count = (int) countt;
				return count;
			}	*/	
}
